package com.le.tester.book.effectivejava3rdchinese.articletwentythree;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

/**
 * createTime：2022/8/9 16:04
 * description：figure
 */
public class Figure {

    enum Shape {RECTANGLE, CIRCLE}

    final Shape shape;

    double length;

    double width;

    double radius;

    Figure(double radius) {
        shape = Shape.CIRCLE;
        this.radius = radius;
    }

    public Figure(double length, double width) {
        shape = Shape.RECTANGLE;
        this.length = length;
        this.width = width;
    }

    public static void main(String[] args) throws IOException {
        String tempFlag = "152";
        String[] split = tempFlag.split(",");
        for (String s : split) {
            System.out.println(s);
        }

        //CollectionUtils.isEmpty()
    }

    /**
     * 获取某文件夹下的文件名和文件内容,存入map集合中
     *
     * @param filePath 需要获取的文件的 路径
     * @return 返回存储文件名和文件内容的map集合
     */
    public static Map<String, String> getFilesData(String filePath) {
        Map<String, String> files = new HashMap<>();
        File file = new File(filePath); //需要获取的文件的路径
        String[] fileNameLists = file.list(); //存储文件名的String数组
        File[] filePathLists = file.listFiles(); //存储文件路径的String数组
        assert filePathLists != null;
        for (int i = 0; i < filePathLists.length; i++) {
            if (filePathLists[i].isFile()) {
                //读取指定文件路径下的文件内容
                String fileDatas = readFile(filePathLists[i]);
                //把文件名作为key,文件内容为value 存储在map中
                assert fileNameLists != null;
                files.put(fileNameLists[i].split("\\.")[0], fileDatas);
            }
        }
        return files;
    }

    //读取文件中的内容
    public static String readFile(File readFiled) {
        //File readFiled = new File(readFileUrl);
        try {
            FileInputStream inReadFile = new FileInputStream(readFiled);
            FileChannel fileChannel = inReadFile.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate((int) readFiled.length());
            //将channel的数据读取进buffer
            fileChannel.read(buffer);
            return new String(buffer.array());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static Map<String, String> mapStringToMap(String str) {
        String[] strs = str.split(",");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            // 去掉头部空格
            String key1 = key.trim();
            String value1 = value.trim();
            map.put(key1, value1);
        }
        return map;
    }

}
